home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / CEGUISDK-0.4.1-VC6-STLport.exe / {app} / Samples / common / src / CEGuiD3D9BaseApplication.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-08-22  |  9.1 KB  |  284 lines

  1. /************************************************************************
  2.     filename:   CEGuiD3D9BaseApplication.cpp
  3.     created:    24/9/2004
  4.     author:     Paul D Turner
  5. *************************************************************************/
  6. /*************************************************************************
  7.     Crazy Eddie's GUI System (http://www.cegui.org.uk)
  8.     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
  9.  
  10.     This library is free software; you can redistribute it and/or
  11.     modify it under the terms of the GNU Lesser General Public
  12.     License as published by the Free Software Foundation; either
  13.     version 2.1 of the License, or (at your option) any later version.
  14.  
  15.     This library is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.     Lesser General Public License for more details.
  19.  
  20.     You should have received a copy of the GNU Lesser General Public
  21.     License along with this library; if not, write to the Free Software
  22.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23. *************************************************************************/
  24. // this controls conditional compile of file for MSVC
  25. #include "CEGUIConfig.h"
  26. #ifdef CEGUI_SAMPLES_USE_DIRECTX_9
  27.  
  28. #include "CEGuiD3D9BaseApplication.h"
  29.  
  30. #include <d3d9.h>
  31. #include <dinput.h>
  32. // undefine Microsoft macro evilness
  33. #undef min
  34. #undef max
  35.  
  36. #include "renderers/directx9GUIRenderer/d3d9renderer.h"
  37. #include "CEGuiSample.h"
  38. #include "Win32AppHelper.h"
  39. #include "CEGUI.h"
  40. #include <stdexcept>
  41.  
  42. #ifdef _MSC_VER
  43. # if defined(DEBUG) || defined (_DEBUG)
  44. #   pragma comment (lib, "DirectX9GUIRenderer_d.lib")
  45. # else
  46. #   pragma comment (lib, "DirectX9GUIRenderer.lib")
  47. # endif
  48. #endif
  49.  
  50. /*************************************************************************
  51.     Impl struct
  52. *************************************************************************/
  53. struct CEGuiBaseApplicationImpl
  54. {
  55.     HWND d_window;
  56.     LPDIRECT3D9 d_D3D;
  57.     LPDIRECT3DDEVICE9 d_3DDevice;
  58.     D3DPRESENT_PARAMETERS d_ppars;
  59.     CEGUI::DirectX9Renderer* d_renderer;
  60.     Win32AppHelper::DirectInputState d_directInput;
  61. };
  62.  
  63.  
  64. /*************************************************************************
  65.     Constructor.
  66. *************************************************************************/
  67. CEGuiD3D9BaseApplication::CEGuiD3D9BaseApplication() :
  68.         pimpl(new CEGuiBaseApplicationImpl),
  69.         d_lastTime(GetTickCount()),
  70.         d_frames(0),
  71.         d_FPS(0)
  72. {
  73.     if (pimpl->d_window = Win32AppHelper::createApplicationWindow(800, 600))
  74.     {
  75.         if (initialiseDirect3D(800, 600, D3DADAPTER_DEFAULT, true))
  76.         {
  77.             if (Win32AppHelper::initialiseDirectInput(pimpl->d_window, pimpl->d_directInput))
  78.             {
  79.                 pimpl->d_renderer = new CEGUI::DirectX9Renderer(pimpl->d_3DDevice, 3000);
  80.  
  81.                 // initialise the gui system
  82.                 new CEGUI::System(pimpl->d_renderer);
  83.  
  84.                 CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::Informative);
  85.  
  86.                 return;
  87.             }
  88.  
  89.             // cleanup direct 3d systems
  90.             pimpl->d_3DDevice->Release();
  91.             pimpl->d_D3D->Release();
  92.         }
  93.  
  94.         DestroyWindow(pimpl->d_window);
  95.     }
  96.     else
  97.     {
  98.         MessageBox(0, Win32AppHelper::CREATE_WINDOW_ERROR, Win32AppHelper::APPLICATION_NAME, MB_ICONERROR|MB_OK);
  99.     }
  100.  
  101.     throw std::runtime_error("Win32 DirectX 9 application failed to initialise.");
  102. }
  103.  
  104.  
  105. /*************************************************************************
  106.     Destructor.
  107. *************************************************************************/
  108. CEGuiD3D9BaseApplication::~CEGuiD3D9BaseApplication()
  109. {
  110.     Win32AppHelper::mouseLeaves();
  111.  
  112.     // cleanup gui system
  113.     delete CEGUI::System::getSingletonPtr();
  114.     delete pimpl->d_renderer;
  115.  
  116.     Win32AppHelper::cleanupDirectInput(pimpl->d_directInput);
  117.  
  118.     // cleanup direct 3d systems
  119.     pimpl->d_3DDevice->Release();
  120.     pimpl->d_D3D->Release();
  121.  
  122.     DestroyWindow(pimpl->d_window);
  123.  
  124.     delete pimpl;
  125. }
  126.  
  127.  
  128. /*************************************************************************
  129.     Start the base application
  130. *************************************************************************/
  131. bool CEGuiD3D9BaseApplication::execute(CEGuiSample* sampleApp)
  132. {
  133.     sampleApp->initialiseSample();
  134.  
  135.     //
  136.     //  This is basically a modified Win32 message pump
  137.     //
  138.     bool idle;
  139.     HRESULT coop;
  140.  
  141.     while (Win32AppHelper::doWin32Events(idle))
  142.     {
  143.         if (idle)
  144.         {
  145.             // handle D3D lost device stuff
  146.             coop = pimpl->d_3DDevice->TestCooperativeLevel();
  147.  
  148.             if (coop == D3DERR_DEVICELOST)
  149.             {
  150.                 Sleep(500);
  151.                 continue;
  152.             }
  153.             else if (coop == D3DERR_DEVICENOTRESET)
  154.             {
  155.                 if (!resetDirect3D())
  156.                 {
  157.                     continue;
  158.                 }
  159.             }
  160.  
  161.             if (FAILED(pimpl->d_3DDevice->BeginScene()))
  162.             {
  163.                 continue;
  164.             }
  165.  
  166.             updateFPS();
  167.             char fpsbuff[16];
  168.             sprintf(fpsbuff, "FPS: %d", d_FPS);
  169.  
  170.             Win32AppHelper::doDirectInputEvents(pimpl->d_directInput);
  171.  
  172.             // draw display
  173.             CEGUI::System& guiSystem = CEGUI::System::getSingleton();
  174.             pimpl->d_3DDevice->Clear(0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  175.             guiSystem.renderGUI();
  176.  
  177.             // render FPS:
  178.             const CEGUI::Font* fnt = guiSystem.getDefaultFont();
  179.             if (fnt)
  180.             {
  181.                 guiSystem.getRenderer()->setQueueingEnabled(false);
  182.                 fnt->drawText(fpsbuff, CEGUI::Vector3(0, 0, 0), guiSystem.getRenderer()->getRect());
  183.             }
  184.  
  185.             pimpl->d_3DDevice->EndScene();
  186.  
  187.             pimpl->d_3DDevice->Present(0, 0, 0, 0);
  188.         }
  189.  
  190.         // check if the application is quitting, and break the loop next time
  191.         // around if so.
  192.         if (isQuitting())
  193.             PostQuitMessage(0);
  194.     }
  195.  
  196.     return true;
  197. }
  198.  
  199.  
  200. /*************************************************************************
  201.     Performs any required cleanup of the base application system.
  202. *************************************************************************/
  203. void CEGuiD3D9BaseApplication::cleanup()
  204. {
  205.     // nothing to do here.
  206. }
  207.  
  208.  
  209. /*************************************************************************
  210.     Initialise Direct3D system.
  211. *************************************************************************/
  212. bool CEGuiD3D9BaseApplication::initialiseDirect3D(unsigned int width, unsigned int height, unsigned int adapter, bool windowed)
  213. {
  214.     pimpl->d_D3D = Direct3DCreate9(D3D_SDK_VERSION);
  215.  
  216.     // display error and exit if D3D creation failed
  217.     if (pimpl->d_D3D)
  218.     {
  219.         D3DDISPLAYMODE d3ddm;
  220.         pimpl->d_D3D->GetAdapterDisplayMode(adapter, &d3ddm);
  221.  
  222.         D3DFORMAT format = d3ddm.Format;
  223.  
  224.         // complete window initialisation
  225.         ShowWindow(pimpl->d_window, SW_NORMAL);
  226.         UpdateWindow(pimpl->d_window);
  227.  
  228.         ZeroMemory(&pimpl->d_ppars, sizeof(pimpl->d_ppars));
  229.         pimpl->d_ppars.BackBufferFormat = format;
  230.         pimpl->d_ppars.hDeviceWindow    = pimpl->d_window;
  231.         pimpl->d_ppars.SwapEffect       = D3DSWAPEFFECT_DISCARD;
  232.         pimpl->d_ppars.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  233.         pimpl->d_ppars.Windowed         = windowed;
  234.  
  235.         if (!windowed)
  236.         {
  237.             pimpl->d_ppars.BackBufferWidth          = width;
  238.             pimpl->d_ppars.BackBufferHeight         = height;
  239.             pimpl->d_ppars.BackBufferCount          = 1;
  240.             pimpl->d_ppars.MultiSampleType          = D3DMULTISAMPLE_NONE;
  241.             pimpl->d_ppars.FullScreen_RefreshRateInHz       = D3DPRESENT_RATE_DEFAULT;
  242.         }
  243.  
  244.         if (SUCCEEDED(pimpl->d_D3D->CreateDevice(adapter, D3DDEVTYPE_HAL, pimpl->d_window, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &pimpl->d_ppars, &pimpl->d_3DDevice)))
  245.         {
  246.             return true;
  247.         }
  248.         else
  249.         {
  250.             MessageBox(0, Win32AppHelper::CREATE_DEVICE_ERROR, Win32AppHelper::APPLICATION_NAME, MB_ICONERROR|MB_OK);
  251.         }
  252.  
  253.         pimpl->d_D3D->Release();
  254.         pimpl->d_D3D = 0;
  255.     }
  256.     else
  257.     {
  258.         MessageBox(0, Win32AppHelper::CREATE_D3D_ERROR, Win32AppHelper::APPLICATION_NAME, MB_ICONERROR|MB_OK);
  259.     }
  260.  
  261.     return false;
  262. }
  263.  
  264.  
  265. /*************************************************************************
  266.     Do reset of Direct3D device
  267. *************************************************************************/
  268. bool CEGuiD3D9BaseApplication::resetDirect3D(void)
  269. {
  270.     // perform ops needed prior to reset
  271.     pimpl->d_renderer->preD3DReset();
  272.  
  273.     if (SUCCEEDED(pimpl->d_3DDevice->Reset(&pimpl->d_ppars)))
  274.     {
  275.         // re-build stuff now reset has been done.
  276.         pimpl->d_renderer->postD3DReset();
  277.  
  278.         return true;
  279.     }
  280.  
  281.     return false;
  282. }
  283.  
  284. #endif